package com.micai.excel.test;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName WriteExcelMain
 * @Description TODO
 * @Author zhaoxinguo
 * @Date 2018/12/11 22:22
 * @Version 1.0
 **/
public class WriteExcelMain {


    public static void main(String [] args) throws IOException {
        writeFileUsingPOI();
    }

    public static void writeFileUsingPOI() throws IOException {
        // create blank workbook
        XSSFWorkbook workbook = new XSSFWorkbook();

        // create a blank sheet
        XSSFSheet sheet = workbook.createSheet("Country");

        List list = new ArrayList<>();
        list.add("China");
        list.add("BeiJin");
        list.add("120000");
        Object[] objects = list.toArray();

        ArrayList<Object[]> data = new ArrayList<Object[]>();
        data.add(new String[]{"Country","Capital","Population"});
        data.add(new Object[]{"India","Delhi",10000});
        data.add(new Object[]{"France","Paris",40000});
        data.add(new Object[]{"Germany","Berlin",20000});
        data.add(new Object[]{"England","London",30000});
        data.add(objects);

        /*ArrayList<Object[]> data2 = new ArrayList<Object[]>();
        data2.add(new String[]{"Country","Capital"});
        data2.add(new Object[]{"India","Delhi"});
        data2.add(new Object[]{"France","Paris"});
        data2.add(new Object[]{"Germany","Berlin"});
        data2.add(new Object[]{"England","London"});

        ArrayList<Object[]> data3 = new ArrayList<Object[]>();
        data3.add(new String[]{"Country"});
        data3.add(new Object[]{"India"});
        data3.add(new Object[]{"France"});
        data3.add(new Object[]{"Germany"});
        data3.add(new Object[]{"England"});*/

        // Iterate over data and write to sheet
        int rownum = 0;
        for (Object[] countries : data) {
            Row row = sheet.createRow(rownum++);

            int cellnum = 0;
            for (Object obj : countries) {
                Cell cell = row.createCell(cellnum++);
                if (obj instanceof String) {
                    cell.setCellValue((String)obj);
                } else if (obj instanceof Double) {
                    cell.setCellValue((Double)obj);
                } else if (obj instanceof Integer) {
                    cell.setCellValue((Integer)obj);
                }
            }
        }
        /*int rownum2 = 7;
        for (Object[] countries : data2) {
            Row row = sheet.createRow(rownum2++);

            int cellnum = 0;
            for (Object obj : countries) {
                Cell cell = row.createCell(cellnum++);
                if (obj instanceof String) {
                    cell.setCellValue((String)obj);
                } else if (obj instanceof Double) {
                    cell.setCellValue((Double)obj);
                } else if (obj instanceof Integer) {
                    cell.setCellValue((Integer)obj);
                }
            }
        }

        int rownum3 = 14;
        for (Object[] countries : data3) {
            Row row = sheet.createRow(rownum3++);

            int cellnum = 0;
            for (Object obj : countries) {
                Cell cell = row.createCell(cellnum++);
                if (obj instanceof String) {
                    cell.setCellValue((String)obj);
                } else if (obj instanceof Double) {
                    cell.setCellValue((Double)obj);
                } else if (obj instanceof Integer) {
                    cell.setCellValue((Integer)obj);
                }
            }
        }*/

        try {
            // write the workbook in file system
            FileOutputStream outputStream = new FileOutputStream(new File("src/main/resources/CountriesDetails.xlsx"));
            workbook.write(outputStream);
            outputStream.close();
            System.out.println("CountriesDetails.xlsx has been created successfully");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            workbook.close();
        }
    }
}
